package com.huiyujinrong.qiangdanbao.activity.my;


import android.content.Context;

import com.huiyujinrong.qiangdanbao.BorrowHelpApp;
import com.huiyujinrong.qiangdanbao.DaggerModelComponent;
import com.huiyujinrong.qiangdanbao.bean.RecordBean;
import com.huiyujinrong.qiangdanbao.model.RecordModel;
import com.huiyujinrong.qiangdanbao.network.Api;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import javax.inject.Inject;

import happyloan.core.RespEvent;

public class RecordPresenter implements RecordContract.Presenter {


    @Inject
    RecordModel model;

    private RecordContract.View view;

    private Context context;

    RecordPresenter(RecordContract.View view, Context context) {
        this.view = view;
        this.context = context;
        setDagger2();
    }

    private void setDagger2() {
        DaggerModelComponent.builder().appComponent(BorrowHelpApp.get().getAppComponent()).build().inject(this);
    }


    @Override
    public void getRecordData(String user_id, int currentPage) {
        model.getRecordData(user_id, currentPage);
    }

    @Override
    public void start() {
        EventBus.getDefault().register(this);
    }

    @Override
    public void end() {
        EventBus.getDefault().unregister(this);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void respEvent(RespEvent<RecordBean> event) {
        if (event.getUrl().equals(Api.getRecordData)) {
            switch (event.getCode()) {
                case RespEvent.Code.SUCCEED:
                    //数据返回成功
                    view.getData(event.getT());
                    break;
                case RespEvent.Code.FAILURE:
                    //数据返回错误
                    if (event.isAlert()) {
                        view.showToast(String.valueOf(event.getExtra()));
                    }
                    break;
                case RespEvent.Code.ERROR:
                    //数据返回异常
                    view.showToast("网络不好,稍后再试");
                    break;
            }
        }
    }
}
